<?php
namespace Operation\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;

class MeteoTable extends AbstractTableGateway
{
    protected $table ='Meteo';
    
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Meteo());
        $this->initialize();
    }
    
    public function getMeteo($where) {
        $rows = array();
        $sql = "SELECT 
                    idMeteoType AS type,
                    date AS meteoDate
                FROM ".$this->table."
                WHERE ".$where;
        //echo $sql.'<br />';
        $result = $this->adapter->query($sql)->execute();
        $return = $result->current();

        return $return;
    }
    
    public function getMeteos($where) {
        $rows = array();
        $sql = "SELECT
                    SUM(idMeteoType) AS nbMeteo,
                    idMeteoType AS type
                FROM ".$this->table."
                WHERE ".$where."
                GROUP BY idMeteoType";
        //echo $sql.'<br />';
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;
        
        return $rows;
    }
    
    public function getSMeteos($where) {
        $rows = array();
        $sql = "SELECT
                    MeteoType.Label AS type,
                    Meteo.date AS meteoDate,
                    Meteo.temperature
                FROM ".$this->table."
                JOIN MeteoType
                    ON(MeteoType.idMeteoType = Meteo.idMeteoType)
                WHERE ".$where."
                GROUP BY Meteo.date";
        //echo $sql.'<br />';
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;
        
        return $rows;
    }
    
    public function getSynthCa($where, $groupby="") {
        $rows = array();
        $sql = "SELECT 
                    SUM(totttc) AS ca_ttc,
                    SUM(totht) AS ca_ht,
                    orderDate
                FROM ".$this->table."
                WHERE ".$where.$groupby;
        
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;
        
        return $rows;
    }
    
    public function getSynthTmHt($where, $groupby="") {
        $sql = "SELECT 
                    SUM(totht) AS valeur,
                    COUNT(idSaleHead) AS nombre,
                    orderDate
                FROM ".$this->table."
                WHERE ".$where.$groupby;
        
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;

        return $rows;
    }
    
    public function getSynthTmTtc($where, $groupby="") {
        $rows = array();
        $sql = "SELECT 
                    SUM(totttc) AS valeur,
                    COUNT(idSaleHead) AS nombre,
                    orderDate
                FROM ".$this->table."
                WHERE ".$where.$groupby;
        
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;

        return $rows;
    }
    
    public function getSynthTransaction($where, $groupby="") {
        $rows = array();
        $sql = "SELECT 
                    COUNT(*) AS nbTransaction,
                    saleHead.orderDate
                FROM ".$this->table."
                WHERE ".$where." AND saleHead.canceled = 0
                ".$groupby;
        
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;

        return $rows;
    }
}
